﻿using ScFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 此类只能当EntityDataManager模块存在时使用
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
public abstract class EntityDataMgr<T1,T2>:LogicDataModuleBase where T1:IVEx,new() where T2:IVEx,new()
{
    protected List<T1> _es1;
    protected List<T2> _es2;
    protected T1 Get1(UInt32 id)
    {
        return _es1.Find(s => s.CompIdentity == id);
    }
    protected T2 Get2(UInt32 id)
    {
        return _es2.Find(s => s.CompIdentity == id);
    }
    protected List<T1> Gets1(ushort eid)
    {
        return _es1.FindAll(s => s.CompIdentity.left() == eid);
    }
    protected List<T2> Gets2(ushort eid)
    {
        return _es2.FindAll(s => s.CompIdentity.left() == eid);
    }
    Type t1, t2;
    public override void OnInit(object args)
    {
        base.OnInit(args);
        t1 = new T1().TargetType;
        t2 = new T2().TargetType;
        _es1 = new List<T1>();
        _es2 = new List<T2>();
    }
    /// <summary>
    /// 第一类组件被创建时
    /// </summary>
    /// <param name="e"></param>
    public virtual void OnCreated1(T1 e)
    {

    }
    /// <summary>
    /// 第二类组件被创建时
    /// </summary>
    /// <param name="e"></param>
    public virtual void OnCreated2(T2 e)
    {

    }
    [RegisterEvent(EntityDataEvent.Created)]
    protected void OnEntityCreated(EntityData r)
    {
        _es1.AddRange(r.GetComponentDatas(t1).ConvertAll(s =>
        {
            var tp = new T1();
            tp.SetData(s);
            OnCreated1(tp);
            return tp;
        }));
        _es2.AddRange(r.GetComponentDatas(t2).ConvertAll(s =>
        {
            var tp = new T2();
            tp.SetData(s);
            OnCreated2(tp);
            return tp;
        }));
    }
    /// <summary>
    /// 当第一类组件被删除时
    /// </summary>
    /// <param name="id"></param>
    public virtual void OnDeleted1(UInt16 eid)
    {
        _es1.RemoveAll(s=>s.CompIdentity.left()==eid);
    }
    /// <summary>
    /// 当第二类组件被删除时
    /// </summary>
    /// <param name="eid"></param>
    public virtual void OnDeleted2(UInt16 eid)
    {
        _es2.RemoveAll(s => s.CompIdentity.left() == eid);
    }
    [RegisterEvent(EntityDataEvent.Deleting)]
    protected void OnEntityDeleted(UInt16 id)
    {
        if(_es1.Find(s => s.CompIdentity.left() == id) != null)
        {
            OnDeleted1(id);
        }
        if (_es2.Find(s => s.CompIdentity.left() == id) != null)
        {
            OnDeleted2(id);
        }
    }
    public virtual void OnValueChanged1(T1 com,ushort vid,object v)
    {

    }
    public virtual void OnValueChanged2(T2 com, ushort vid, object v)
    {

    }
    [RegisterEvent(EntityDataEvent.ValueChanged)]
    protected void OnEntityValueChanged(ValueChangeArgs args)
    {
        var tp1 = _es1.Find(s => s.CompIdentity == args.EntityID.combine(args.ValueID.left()));
        if (tp1 != null)
        {
            OnValueChanged1(tp1, args.ValueID.right(), args.Value);
        }
        var tp2 = _es2.Find(s => s.CompIdentity == args.EntityID.combine(args.ValueID.left()));
        if (tp2 != null)
        {
            OnValueChanged2(tp2, args.ValueID.right(), args.Value);
        }

    }

}
